package com.kgc.controller;

import com.kgc.bean.GoodsCarinfo;
import com.kgc.bean.User;
import com.kgc.beanForm.GoodsCarinfoForm;


import com.kgc.config.annotation.RequestPermission;
import com.kgc.config.annotation.RequestUser;
import com.kgc.service.CarService;
import com.kgc.util.RedisUtils;
import com.kgc.util.ResultBuildUtil;
import com.kgc.util.ReturnResult;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.Map;

@RestController
@Slf4j
@Api(tags = "购物车功能")
public class CarController {
    @Autowired
    private CarService carService;


    //1.加入购物车功能
    @RequestPermission
    @PostMapping("/addToShoppingCart")
    @ApiOperation(value = "加入购物车",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "加入购物车成功"),
            @ApiResponse(code = 3002,message = "token错误，重新登录"),
            @ApiResponse(code = 3003,message = "该商品不存在")
    })
    ReturnResult<Map<String,Object>> addToShoppingCart(@Valid GoodsCarinfoForm goodsCarinfoForm, @RequestUser @ApiIgnore User user, HttpServletRequest request,@ApiIgnore Integer businessid){
        if (null==user||user.getUserId()==null){
            ResultBuildUtil.fail("3002","token错误，重新登录");
        }
        log.info("**************user:{}*****************",user.toString());
        GoodsCarinfo goodsCarinfo=new GoodsCarinfo();
        goodsCarinfo.setGoodsid(goodsCarinfoForm.getGoodsid());
        goodsCarinfo.setQuantity(goodsCarinfoForm.getQuantity());
        String goodsId = goodsCarinfoForm.getGoodsid();
        return carService.addToShoppingCart(goodsCarinfo,user,request,goodsId,businessid);
    }
    //2.查看购物车功能
    @RequestPermission
    @PostMapping("/checkTheShoppingCart")
    @ApiOperation(value = "查看购物车",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "成功"),
            @ApiResponse(code = 3002,message = "token错误，重新登录")
    })
    ReturnResult<Map<String,Object>> checkTheShoppingCart(@RequestUser @ApiIgnore User user,@ApiIgnore String goodsId,@ApiIgnore Integer businessid){
        if (null==user||user.getUserId()==null){
            ResultBuildUtil.fail("3002","token错误，重新登录");
        }
        return carService.checkTheShoppingCart(user,goodsId,businessid);
    }
    //3.删除购物车某个商品
    @RequestPermission
    @PostMapping("/deleteAnItemInTheShoppingCart")
    @ApiOperation(value = "删除购物车某个商品",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "成功"),
            @ApiResponse(code = 3002,message = "token错误，重新登录"),
            @ApiResponse(code = 3003,message = "该商品不存在"),
            @ApiResponse(code = 3004,message = "购物车中无该商品，删除失败")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "goodsId", value = "商品id", required = true)
    })
    ReturnResult<Map<String,Object>> deleteAnItemInTheShoppingCart(String goodsId,@RequestUser @ApiIgnore User user){
        if (null==user||user.getUserId()==null){
            ResultBuildUtil.fail("3002","token错误，重新登录");
        }
        return carService.deleteAnItemInTheShoppingCart(goodsId,user);
    }
    //4.清空购物车
    @RequestPermission
    @PostMapping("/emptyShoppingCart")
    @ApiOperation(value = "清空购物车",notes = "")
    @ApiResponses({
            @ApiResponse(code = 0000,message = "成功"),
            @ApiResponse(code = 3001,message = "购物车清除失败！"),
            @ApiResponse(code = 3002,message = "token错误，重新登录")

    })
    ReturnResult<Map<String,Object>> emptyShoppingCart(@RequestUser @ApiIgnore User user,String goodsId,@ApiIgnore Integer businessid){
        if (null==user||user.getUserId()==null){
            ResultBuildUtil.fail("3002","token错误，重新登录");
        }
        return carService.emptyShoppingCart(user,goodsId,businessid);
    }

}
